﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.Windows.Media.Imaging;
using System.IO;

namespace MobSoulWPhoneToolkit.Classes
{
    public class SaveLoadJPEGService
    {
        /// <summary>
        /// Save a BitmapImage in the IsolatedStorageFile.
        /// </summary>
        /// <param name="image">The BitmapImage</param>
        /// <param name="name">File's name</param>
        public static void Save(BitmapImage image, string name)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                WriteableBitmap bmImage = new WriteableBitmap(image);

                using (IsolatedStorageFileStream isoStream = store.OpenFile(name, FileMode.OpenOrCreate))
                {
                    Extensions.SaveJpeg(bmImage, isoStream, bmImage.PixelWidth, bmImage.PixelHeight, 0, 100);
                    isoStream.Close();
                }

            }
        }

        /// <summary>
        /// Save a BitmapImage in the IsolatedStorageFile.
        /// </summary>
        /// <param name="image">The BitmapImage</param>
        /// <param name="name">Directory's name(write only the name)</param>
        /// <param name="c">File's name</param>
        public static void Save(BitmapImage image, string directory, string name)
        {
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                WriteableBitmap bmImage = new WriteableBitmap(image);

                using (IsolatedStorageFileStream isoStream = store.OpenFile("\\" + directory + "\\" + name, FileMode.OpenOrCreate))
                {
                    Extensions.SaveJpeg(bmImage, isoStream, bmImage.PixelWidth, bmImage.PixelHeight, 0, 100);
                    isoStream.Close();
                }
            }
        }

        /// <summary>
        /// Load a BitmapImage in the specific path IsolatedStorageFile.
        /// </summary>
        /// <param name="image">Write the fil's path</param>
        /// <returns>Return the BitmapImage</returns>
        public static BitmapImage Load(string path)
        {
            BitmapImage image;
            using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (store.FileExists(path) == true)
                {
                    using (IsolatedStorageFileStream isoStream = store.OpenFile(path, FileMode.Open))
                    {
                        image = new BitmapImage();
                        image.SetSource(isoStream);
                    }
                    return image;
                }
                else
                    return null;
            }
        }
    }
}
